import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:to_do/widgets/home_screen_layout.dart';
import '../widgets/modal_bottom_sheet.dart';
import '../constants/colors.dart';
import 'package:provider/provider.dart';
import '../util/to_do.dart';

// ignore: must_be_immutable
class HomeScreen extends StatelessWidget {
  String day;
  int date;
  String month;

  void setDayAndDate() {
    date = DateTime.now().day;
    var weekday = DateTime.now().weekday;

    switch (weekday) {
      case 1:
        day = '星期一';
        break;
      case 2:
        day = '星期二';
        break;
      case 3:
        day = '星期三';
        break;
      case 4:
        day = '星期四';
        break;
      case 5:
        day = '星期五';
        break;
      case 6:
        day = '星期六';
        break;
      case 7:
        day = '星期日';
    }
  }

  void setMonth() {
    var m = DateTime.now().month;
    switch (m) {
      case 1:
        month = '一月';
        break;
      case 2:
        month = '二月';
        break;
      case 3:
        month = '三月';
        break;
      case 4:
        month = '四月';
        break;
      case 5:
        month = '五月';
        break;
      case 6:
        month = '六月';
        break;
      case 7:
        month = '七月';
        break;
      case 8:
        month = '八月';
        break;
      case 9:
        month = '九月';
        break;
      case 10:
        month = '十月';
        break;
      case 11:
        month = '十一月';
        break;
      case 12:
        month = '十二月';
    }
  }

  @override
  Widget build(BuildContext context) {
    setDayAndDate();
    setMonth();
    Provider.of<ToDo>(context, listen: false).initPrefs();
    return Consumer<ToDo>(
      builder: (context, child, toDo) {
        var toDo = Provider.of<ToDo>(context, listen: true);
        return Scaffold(
          backgroundColor: kBackgroundColor,
          appBar: AppBar(
              title: Text(
                '$day, $date $month',
                style:
                    TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
              ),
              centerTitle: true,
              backgroundColor: kBackgroundColor,
              elevation: 0),
          floatingActionButton: FloatingActionButtons(),
          body: Container(
            height: double.infinity,
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.all(15),
                child: HomeScreenLayout(),
              ),
            ),
          ),
        );
      },
    );
  }
}

class FloatingActionButtons extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        FloatingActionButton.extended(
          onPressed: () => showModalBottomSheet(
              backgroundColor: kBackgroundColor,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              context: context,
              builder: (context) => ModalBottomSheet(
                    type: 'homeScreen',
                  )),
          label: Text('+ 添加一个新任务组'),
        ),
        FloatingActionButton(
            backgroundColor: kBlue,
            child: Icon(Icons.add),
            onPressed: () => showModalBottomSheet(
                backgroundColor: kBackgroundColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10.0),
                ),
                context: context,
                builder: (context) => ModalBottomSheet(type: 'homeAdd')))
      ],
    );
  }
}
